#region Licence
// Copyright (c) 2008 Tasman Logic Ltd

// Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
// and associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all copies 
// or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
// NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;

namespace ActiveSharp.PropertyMapping
{
    /// <summary>
    /// A wrapper around our offset calculation code.  The actual offset calculation code is
    /// in a dynamically generated assembly, this class is just a wrapper to make it easier to call.
    /// </summary>
    /// <remarks>Main job of this class is to ensure that we generate the dynamic assembly exactly once</remarks>
    public static class OffsetUtil
    {
        /// <summary>
        /// Find the position, in memory, of a field relative to the object that contains it.
        /// It is up to the caller to make sure to pass a <paramref name="field"/> that really is in <paramref name="containingObject"/>
        /// </summary>
        public static int GetOffset<T>(object containingObject, ref T field)
        {
            return OffsetInvoker<T>.GetOffset(containingObject, ref field);
        }

        #region implementation

        static Type OffsetUtilType = GetDynamicType();          // static, so that we make the dynamic assembly only once

        static Type GetDynamicType()
        {
            Type result;
            OffsetFinderGenerator.BuildOffsetGetter(out result);
            return result;
        }

        /// <summary>
        /// A helper method to help is invoke the generic method that we are targeting, while only
        /// resorting to reflection once (for each T)
        /// </summary>
        /// <remarks>Main job of this class is to ensure that we generate the typed delegate exactly once for each T</remarks>
        private static class OffsetInvoker<T>
        {
            // static, so that we do the reflection, to create the delegate, just once for each T
            static OffsetFinderDelegate<T> d = (OffsetFinderDelegate<T>)Delegate.CreateDelegate(
                typeof(OffsetFinderDelegate<T>), 
                null,
                OffsetUtilType.GetMethod("GetOffset").MakeGenericMethod(typeof(T)));

            public static int GetOffset(object containingObject, ref T field)
            {
                return d(containingObject, ref field);
            }
        }

        #endregion

    }
}
